package com.mctuoluo.mappay.sql;

import com.mctuoluo.mappay.MapPay;
import com.mctuoluo.supersql.CreatePool;
import org.bukkit.Bukkit;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class sql {
    private static MapPay mapPay = MapPay.getMapPay();
    private static CreatePool createPool = new CreatePool();
    private static Connection conn ;
    private static Statement statement;
    private static ResultSet rs;


    public void sendOrder(String name){
        try {
            conn = CreatePool.getHiDB();
            statement = conn.createStatement();
            String sql = "select * from mappay_order where pay_id='" + name + "' ";//查询玩家是否有数据！
            rs = statement.executeQuery(sql);
            if (!rs.next()) {
                rs.close();
                statement.close();
                conn.close();
                Bukkit.getPlayer(name).sendMessage("§b暂时没有查询到订单！");
            } else {
                if (rs.getInt("status") == 2){
                    System.out.println("points give "+name+" "+ String.valueOf(rs.getDouble("price") *100 ));

                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"points give "+name+" "+ String.valueOf(rs.getInt("price") *100 ));
                    Bukkit.getPlayer(name).sendMessage("§b查询到一个点券订单，已充值 "+String.valueOf(rs.getDouble("price") *100+"点券！"));
                    setOrder(rs.getInt("id"));
                }
                while (rs.next()){
                    if (rs.getInt("status") == 2){
                        System.out.println("points give "+name+" "+ String.valueOf(rs.getDouble("price") *100 ));
                        Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"points give "+name+" "+ String.valueOf(rs.getInt("price") *100 ));
                        Bukkit.getPlayer(name).sendMessage("§b查询到一个点券订单，已充值 "+String.valueOf(rs.getDouble("price") *100+"点券！"));
                        setOrder(rs.getInt("id"));
                    }
                }
                Bukkit.getPlayer(name).sendMessage("§b您的订单已全部到账！");
                rs.close();
                statement.close();
                conn.close();

            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
    }
    public boolean sendOrders(String name){
        boolean s = false;
        try {
            conn = CreatePool.getHiDB();
            statement = conn.createStatement();
            String sql = "select * from mappay_order where pay_id='" + name + "' ";//查询玩家是否有数据！
            rs = statement.executeQuery(sql);
            if (!rs.next()) {
                rs.close();
                statement.close();
                conn.close();
                return false;
            } else {
                if (rs.getInt("status") == 2){
                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"points give "+name+" "+ String.valueOf((int)rs.getDouble("price") *100 ));
                    Bukkit.getPlayer(name).sendMessage("§b查询到一个点券订单，已充值 "+String.valueOf(rs.getDouble("price") *100+"点券！"));
                    s = setOrder(rs.getInt("id"));
                }
                while (rs.next()){
                    if (rs.getInt("status") == 2){

                        Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"points give "+name+" "+ String.valueOf((int) (rs.getDouble("price") *100)));
                        Bukkit.getPlayer(name).sendMessage("§b查询到一个点券订单，已充值 "+String.valueOf(rs.getDouble("price") *100+"点券！"));
                        s = setOrder(rs.getInt("id"));

                    }
                }


                Bukkit.getPlayer(name).sendMessage("§b您的订单已全部到账！");

                rs.close();
                statement.close();
                conn.close();
                return s;

            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        return false;
    }

    public boolean setOrder(int id) throws SQLException {
        conn = CreatePool.getHiDB();
        statement = conn.createStatement();
        String sql = "UPDATE mappay_order SET status = '" +3+ "' WHERE id = '" +id + "'";
        statement.executeUpdate(sql);
        return true;
    }

}
